package com.baihan.angus.app.game.busshow.ui.script_library.script_rank;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baihan.angus.app.game.basic.base.BaseFragment;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.RankingAdp;
import com.baihan.angus.app.game.busshow.databinding.FragmentRankingBinding;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.bean.response.RankingResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * 排行榜
 */
public class RankingFragment extends BaseFragment<FragmentRankingBinding, RankingVM> {

    private static final String ARG_PARAM1 = "param1";

    private RankingAdp mRankingAdp;
    private Map<String, Object> mRankingRequest = new HashMap<>();

    private String rankingTitle;
    private String tag;
    private int mPage = 1;
    private int mSize = 10;
    private int totalPage = 0;
    private String linkUrl;

    public RankingFragment() {
        // Required empty public constructor
    }

    public static RankingFragment newInstance(String param1) {
        RankingFragment fragment = new RankingFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initBundleData() {
        rankingTitle = (String) getArguments().getSerializable(ARG_PARAM1);
    }

    @Override
    protected int getViewModelId() {
        return BR.rankingVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_ranking;
    }

    @Override
    protected void initView() {

        mRankingAdp = new RankingAdp();
        mVDBinding.rvRanking.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        mRankingAdp.bindToRecycleView(mVDBinding.rvRanking);

        mVDBinding.srlRanking.setOnRefreshListener(refreshLayout -> {
            mPage = 1;
            mRankingRequest.put("page", mPage);
            mViewModel.getRankList(mRankingRequest);
        });

        mVDBinding.srlRanking.setOnLoadMoreListener(refreshLayout -> {
            mRankingRequest.put("page", ++mPage);
            mViewModel.getRankList(mRankingRequest);
        });

        mRankingAdp.setOnItemClickListener(new RankingAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                linkUrl = "http://h5.hqzl.icu/#/?id=" + mRankingAdp.getItem(position).getScript_issue_id();
                ARouter.getInstance().build(ArouterAppPath.WEBVIEW_ACTIVITY).withString("linkUrl", linkUrl).navigation();
            }
        });

    }

    @Override
    protected void initData() {
        if (rankingTitle.equals("新品榜")) {
            tag = "newest";
        } else if (rankingTitle.equals("神作榜")) {
            tag = "god";
        } else if (rankingTitle.equals("热销榜")) {
            tag = "hot";
        }

        mRankingRequest.put("tag", tag);
        mRankingRequest.put("size", mSize);
        mRankingRequest.put("page", mPage);
        mRankingRequest.put("order", "hits desc");
        mViewModel.getRankList(mRankingRequest);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldRanking.observe(this, new Observer<RankingResponse.DataX>() {
            @Override
            public void onChanged(RankingResponse.DataX dataX) {
                if (mVDBinding.srlRanking.isRefreshing()) mVDBinding.srlRanking.finishRefresh();
                if (mVDBinding.srlRanking.isLoading()) mVDBinding.srlRanking.finishLoadMore();

                if (dataX == null) return;

                if (mPage == 1) {
                    mRankingAdp.setNewData(dataX.getData());
                } else {
                    mRankingAdp.addAll(dataX.getData());
                }

                if (dataX.getTotal() % mSize > 0) {
                    totalPage = dataX.getTotal() / mSize + 1;
                } else {
                    totalPage = dataX.getTotal() / mSize;
                }

                if (mPage < totalPage) {
                    mVDBinding.srlRanking.setEnableLoadMore(true);
                } else {
                    mVDBinding.srlRanking.setEnableLoadMore(false);
                }
            }
        });
    }
}